<?php
declare (strict_types = 1);

namespace app\controller;

use app\middleware\Auth as AuthMiddleware;
use think\Request;
use app\validate\User as UserValidate;
class Host
{
    protected $middleware = [AuthMiddleware::class];
    private $toast = 'public/toast';

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        return view('index',[
            'list'  =>   \app\model\Host::withSearch(['id', 'host','create_at'], [
                'id'        =>  request()->param('id'),
                'host'      =>  request()->param('host'),
                'create_at'   =>  request()->param('create_at')
            ])->paginate([
                'list_rows'     =>  20,
                'query'         =>  request()->param()
            ]),
            'orderTime'         =>  request()->param('create_time') == 'desc' ? 'asc' : 'desc'
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
        return view('create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //

        $data = $request->param();
        try {
            validate(UserValidate::class)->scene('host')->batch(true)->check($data);
        } catch (ValidateException $exception) {
            return view($this->toast, [
                'infos'     =>  $exception->getError(),
                'url_text'  =>  '返回上一页',
                'url_path'  =>  url('/host/create')
            ]);
        }
        //写入数据库
        $id = \app\model\Host::create($data)->getData('id');

        $type = ['香港六合彩','澳门六合彩','北京pk10','江苏快三','加拿大28'];
        $url = ['hk','am','bjpk','jsks','jnd'];
        //返回
        $hostc = new \app\model\Hostcat();
        $hostinfo = new \app\model\Hostinfo();
        for ($i = 0;$i<count($type);$i++){
            $list[$i] = ['host_id'=>$id,'name'=>$type[$i],'url'=>$url[$i]];
        }
        $hostcat = $hostc->saveAll($list);
        for ($i = 0;$i<count($hostcat);$i++){
            $lists[$i] = ['hostcat_id'=>$hostcat[$i]['id']];
        }
        $hosti = $hostinfo->saveAll($lists);
        return $id && $data && $hostcat && $hosti? view($this->toast, [
            'infos'     =>  ['添加成功~'],
            'url_text'  =>  '返回列表页',
            'url_path'  =>  url('/host')
        ]) : '添加失败';
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //

        return view('edit', [
            'obj'   =>  \app\model\Host::find($id)
        ]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
        $data = $request->param();
        try {
            validate(UserValidate::class)
                ->scene('host')
                ->batch(true)
                ->check($data);
        } catch (ValidateException $exception) {
            return view($this->toast, [
                'infos'     =>  $exception->getError(),
                'url_text'  =>  '返回上一页',
                'url_path'  =>  url('/host/'.$id.'/edit')
            ]);
        }

        return \app\model\Host::update($data) ? view($this->toast, [
            'infos'     =>  ['恭喜，修改成功~'],
            'url_text'  =>  '返回列表页',
            'url_path'  =>  url('/host')
        ]) : '修改失败';
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
        return \app\model\Host::destroy($id) ? view($this->toast, [
            'infos'     =>  ['恭喜，删除成功~'],
            'url_text'  =>  '返回列表页',
            'url_path'  =>  url('/host')
        ]) : '删除失败！';
    }
}
